package com.cms.recursoshumanos.facade;

import java.io.UnsupportedEncodingException;

import net.rim.device.api.ui.UiApplication;

import com.cms.recursoshumanos.cache.ListaSolicitudCompraCache;
import com.cms.recursoshumanos.constant.Constant;
import com.cms.recursoshumanos.engine.ScreenEngine;
import com.cms.recursoshumanos.handler.WServiceCall;
import com.cms.recursoshumanos.popup.ShowDialogAlert;
import com.cms.recursoshumanos.transport.BBRequest;
import com.cms.recursoshumanos.transport.BBRequestQueue;
import com.cms.recursoshumanos.transport.HttpRequest;
import com.cms.recursoshumanos.ui.catalogo.solicitud_compra.DetailSolicitudCompraScreen;

public class CancelSolicitudCompraFacade extends BaseFacade implements BBRequest.Listener
{

	private String strUsername;
	private String strPass;
	private int intIdSolicitud;
	private String strMotivo;

	private HttpRequest req;
	
	ShowDialogAlert alerta;
	
	public CancelSolicitudCompraFacade(String user,
												String password,
												int id_solicitud,
												String motivo)
	{
		this.strUsername = user;
		this.strPass = password;
		this.intIdSolicitud = id_solicitud;
		this.strMotivo = motivo;
	}

	public void getCancelarSolicitud()
	{
		enviarSolicitud( WServiceCall.getInstance().cancelarSolicitudCompra(
																				strUsername, 
																				strPass,
																				intIdSolicitud,
																				strMotivo));
	}
	
	public void enviarSolicitud(String envelope_soap) 
	{
		try 
		{
			req = new HttpRequest();
			req.setRequestURL(Constant.URL_WEBSERVICE);	
			req.setPayload(envelope_soap, "UTF-8");
			req.setListener(this);
	        BBRequestQueue.getInstance().addRequest(req);
		} 
		catch (UnsupportedEncodingException e) 
		{
			e.printStackTrace();
		} 
		catch (Exception e)
		{
			e.printStackTrace();
		}
		
        
	}
	
	public void requestStarted(BBRequest request) 
	{
		synchronized (UiApplication.getEventLock()) 
		{
			ScreenEngine.getInstance().showLoadingPopup("Cargando");
		}
		
	}
	
	public void requestCancelled(BBRequest request) 
	{
		ScreenEngine.getInstance().hideLoadingPopup();
	}

	public void requestFailed(BBRequest request)
	{
		ScreenEngine.getInstance().hideLoadingPopup();
	}

	public void requestSucceeded(BBRequest request) 
	{
		
		ScreenEngine.getInstance().hideLoadingPopup();
		alerta = new ShowDialogAlert("Se ha cancelado la Solicitud");
		alerta.run();
		synchronized (UiApplication.getEventLock()) 
		{
			ScreenEngine.getInstance().popScreen(DetailSolicitudCompraScreen.getInstance(ListaSolicitudCompraCache.getInstance().getListaSolicitudCompra().getSolicitudById(intIdSolicitud)));
		}
	}
	
	public String setValores(String strResult)
	{
		if (strResult == null || strResult =="")
		{
			return Constant.CODE_NOT_SERVER_RESPONSE;
		}
		else
		{
			return Constant.CODE_OK;
		}
	}
	

	
}

